app.controller('BlogListController', [
  '$scope', 'Blog', 'blogList', '$location', function($scope, Blog, blogList, $location) {
    $scope.blogListClass = 'active';
    $scope.blogList = blogList;
    return $scope.detailView = function(blog) {
      return $location.path('/blog/detail/' + blog.id);
    };
  }
]);

app.controller('BlogDetailController', [
  '$scope', 'Blog', 'blog', '$timeout', '$alert', '$location', function($scope, Blog, blog, $timeout, $alert, $location) {
    $scope.blogListClass = 'active';
    $scope.blog = blog;
    $scope.save = function() {
      return new Blog($scope.blog).$save({
        id: blog.id
      }, function() {
        return $alert({
          title: '保存成功',
          placement: 'top-right',
          type: 'info',
          show: true,
          duration: 2
        });
      });
    };
    return $scope["delete"] = function() {
      if (!confirm('确定删除此文章')) {
        return;
      }
      return new Blog($scope.blog).$remove({
        id: blog.id
      }, function() {
        var alert;
        alert = $alert({
          title: '删除成功',
          content: '准备跳转到列表页..',
          placement: 'top-right',
          type: 'info',
          show: true
        });
        return $timeout(function() {
          alert.hide();
          return $location.path('/blog/list');
        }, 2000);
      });
    };
  }
]);

app.controller('BlogNewController', [
  '$scope', 'Blog', '$alert', '$timeout', '$location', function($scope, Blog, $alert, $timeout, $location) {
    $scope.blogNewClass = 'active';
    return $scope.save = function() {
      return new Blog($scope.blog).$save(function() {
        var alert;
        alert = $alert({
          title: '保存成功',
          content: '准备跳转到列表页..',
          placement: 'top-right',
          type: 'info',
          show: true
        });
        return $timeout(function() {
          alert.hide();
          return $location.path('/blog/list');
        }, 2000);
      });
    };
  }
]);

app.config([
  '$routeProvider', function($routeProvider) {
    $routeProvider.when('/blog/list', {
      templateUrl: '/admin/tmpl/blog_list',
      controller: 'BlogListController',
      resolve: {
        blogList: function(Blog) {
          return Blog.query();
        }
      }
    });
    $routeProvider.when('/blog/new', {
      templateUrl: '/admin/tmpl/blog_new',
      controller: 'BlogNewController'
    });
    $routeProvider.when('/blog/detail/:id', {
      templateUrl: '/admin/tmpl/blog_detail',
      controller: 'BlogDetailController',
      resolve: {
        blog: function(Blog, $route) {
          return Blog.get({
            id: $route.current.params.id
          });
        }
      }
    });
    return $routeProvider.when('/blog', {
      redirectTo: '/blog/list'
    });
  }
]);
